/*
 * Code for user interface menus
 */
#include <stdio.h>
#include <spr.h>
#include <hw.h>
#include <display.h>
#include <lang.h>
#include <melodies.h>
#include <auplay.h>
#include <keypad.h>
#include <config.h>

#include <ui.h>
#include <uiSpeedDial.h>
#include <uiPhoneBook.h>
#include <uiTestMain.h>
#include <zcom_uiCallHistory.h>
#include <phonebook.h>
#include <speeddial.h>

#include "zcom_uiModelCapability.h"
//by Mike
#include "zcom_uiIMMain.h"

/* Main Menu->Call log *******************************************************/
static const UIMenuNode MenuCalllogNodes[] = {
    {
        .text        = {LANG_Menu_Calllog_missedcalls},
        .selectCB    = zcom_uiCallHistoryMissedCallStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Menu_Calllog_receivedcalls},
        .selectCB    = zcom_uiCallHistoryReceivedCallStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Menu_Calllog_dialedcalls},
        .selectCB    = zcom_uiCallHistoryDialedCallStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Menu_Calllog_deleteall},
        .selectCB    = zcom_uiCallHistoryDeleteAllCallLogsStart,
        .selectCBarg = NULL
    },
};

static const UIMenuListInit MenuMenuCalllog = {
    /*
     * Main menu title
     */
    .title = { LANG_Menu_Calllog_title },
    /*
     * Number of menu nodes
     */
    .nNodes = sizeof(MenuCalllogNodes) / sizeof(MenuCalllogNodes[0]),
    /*
     * Current node.  0 => top
     */
    .currNode = 0,
    /*
     * Display lines.  0 => dynamic
     */
    .nDispLines = 0,
    /*
     * Menu nodes
     */
    .nodes = MenuCalllogNodes
};

static void uiMenuCalllogStart(void *data)
{
    uiMenuListInit(&MenuMenuCalllog);
}

/* Main Menu->Setup **********************************************************/
extern void zcom_uiCallSettingStart(void *data);
static const UIMenuNode MenuSetupNodes[] = {
	#if CFG_ALARM_ENABLE()
    {
        .text        = {LANG_Menu_Setup_alarmclock},
        .selectCB    = uiAlarmStart,
        .selectCBarg = NULL
    },
    #endif
    {
        .text        = {LANG_Menu_Setup_datetime},
        .selectCB    = (UICBSelect*)uiMenuListInit,
        .selectCBarg = (void*)&uiClockDateTimeSet
    },
    {
        .text        = {LANG_Zcom_Menu_PhoneSetting_title},
        .selectCB    = zcom_uiPhoneSettingStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Zcom_Menu_CallSetting_title},
        .selectCB    = zcom_uiCallSettingStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Zcom_Menu_Information_title},
        .selectCB    = zcom_uiInformationStart,
        .selectCBarg = NULL
    },
};

static const UIMenuListInit MenuMenuSetup = {
    /*
     * Main menu title
     */
    .title = { LANG_Zcom_Menu_Setup },
    /*
     * Number of menu nodes
     */
    .nNodes = sizeof(MenuSetupNodes) / sizeof(MenuSetupNodes[0]),
    /*
     * Current node.  0 => top
     */
    .currNode = 0,
    /*
     * Display lines.  0 => dynamic
     */
    .nDispLines = 0,
    /*
     * Menu nodes
     */
    .nodes = MenuSetupNodes
};

static void uiMenuSetupStart(void *data)
{
    uiMenuListInit(&MenuMenuSetup);
}

/* Main Menu->Network->WLAN Profiles *****************************************/
extern void zcom_uiWlanAutoConnectStart(void *data);
extern void zcom_uiWlanSimleConfigStart(void *data);
static const UIMenuNode zcom_MenuWLANProfilesNodes[] = {
    {
        .text        = {LANG_Zcom_Menu_WLAN_Profiles_Search},
        .selectCB    = zcom_uiWlanProfileList,
        .selectCBarg = NULL
    },
//    {
//        .text        = {LANG_Zcom_Menu_WLAN_Auto_Connect},
//        .selectCB    = zcom_uiWlanAutoConnectStart,
//        .selectCBarg = NULL
//    },
    {
        .text        = {LANG_Zcom_Menu_WLAN_Profiles_Add},
        .selectCB    = zcom_uiWlanProfileAdd,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Menu_Setup_wlan_autoconfig},
        .selectCB    = (UICBSelect*)zcom_uiWlanSimleConfigStart,
        .selectCBarg = NULL
    },

};

static const UIMenuListInit zcom_MenuWLANProfiles = {
    /*
     * Main menu title
     */
    .title = { LANG_Zcom_Menu_WLAN_Profiles_title },
    /*
     * Number of menu nodes
     */
    .nNodes = sizeof(zcom_MenuWLANProfilesNodes) / sizeof(zcom_MenuWLANProfilesNodes[0]),
    /*
     * Current node.  0 => top
     */
    .currNode = 0,
    /*
     * Display lines.  0 => dynamic
     */
    .nDispLines = 0,
    /*
     * Menu nodes
     */
    .nodes = zcom_MenuWLANProfilesNodes
};

/* Main Menu->Network->SIP Profiles ******************************************/
static const UIMenuNode zcom_MenuSIPProfilesNodes[] = {
    {
        .text        = {LANG_Zcom_Menu_SIP_Profiles_Search},
        .selectCB    = zcom_uiSipProfileList,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Zcom_Menu_SIP_Profiles_Add},
        .selectCB    = zcom_uiSipProfileAdd,
        .selectCBarg = NULL
    },
};

static const UIMenuListInit zcom_MenuSIPProfiles = {
    /*
     * Main menu title
     */
    .title = { LANG_Zcom_Menu_SIP_Profiles_title },
    /*
     * Number of menu nodes
     */
    .nNodes = sizeof(zcom_MenuSIPProfilesNodes) / sizeof(zcom_MenuSIPProfilesNodes[0]),
    /*
     * Current node.  0 => top
     */
    .currNode = 0,
    /*
     * Display lines.  0 => dynamic
     */
    .nDispLines = 0,
    /*
     * Menu nodes
     */
    .nodes = zcom_MenuSIPProfilesNodes
};

/* Main Menu->Network ********************************************************/
extern void zcom_uiPingTestStart(void *unused);
void uiMenuSIPProfileStart(void *data)
{
    uiMenuListInit(&zcom_MenuSIPProfiles);
}

static void uiMenuNetworkStart(void *data)
{
    int nNodes = 0;
    static UIMenuNode menuSelectNodes[5];
    UIMenuListInit init;

   /*
    * Init menu Nodes
    */
    memset(menuSelectNodes, 0, sizeof(menuSelectNodes));

    menuSelectNodes[nNodes].text.textIdx = LANG_Zcom_Menu_Site_Scan;
    menuSelectNodes[nNodes].selectCB     = zcom_uiWlanStateScanListMenuStart;
    menuSelectNodes[nNodes].selectCBarg  = NULL;
    nNodes++;

    menuSelectNodes[nNodes].text.textIdx = LANG_Zcom_Menu_WLAN_Profiles;
    menuSelectNodes[nNodes].selectCB     = (UICBSelect*)uiMenuListInit;
    menuSelectNodes[nNodes].selectCBarg  = (void *)&zcom_MenuWLANProfiles;
    nNodes++;

    if (!zcom_uiModelCapabilityIsEnabled(eModelCapability_SIPProfileHidden))
    {
        menuSelectNodes[nNodes].text.textIdx = LANG_Zcom_Menu_SIP_Profiles;
        menuSelectNodes[nNodes].selectCB     = uiMenuSIPProfileStart;
        menuSelectNodes[nNodes].selectCBarg  = NULL;
        nNodes++;
    }

    menuSelectNodes[nNodes].text.textIdx = LANG_Zcom_Menu_Ping_Test;
    menuSelectNodes[nNodes].selectCB     = zcom_uiPingTestStart;
    menuSelectNodes[nNodes].selectCBarg  = NULL;
    nNodes++;

    menuSelectNodes[nNodes].text.textIdx = LANG_Zcom_Menu_Reconnect;
    menuSelectNodes[nNodes].selectCB     = (UICBSelect*)zcom_ReconnectStart;
    menuSelectNodes[nNodes].selectCBarg  = NULL;
    nNodes++;

   /*
    * Init menu List
    */
    memset(&init, 0, sizeof(init));
    init.title.textIdx  = LANG_Zcom_Menu_Network_title;
    init.nodes          = menuSelectNodes;
    init.nNodes         = nNodes;
    init.autoAdvance    = 0;

   /*
    * Init menu to active
    */
    uiMenuListInit(&init);
}

/* Main Menu ****************************************************************/
extern void zcom_uiProfilesSettingStart(void *data);
extern void zcom_uiTestFlashWrite(void* unused);
extern void zcom_uiTestFlashRead(void* unused);
extern void uiMenuGtalkStart(void* data);
static const UIMenuNode MenuMainNodes[] = {
    {
        .text        = {LANG_Zcom_Menu_Call_Log},
        .selectCB    = uiMenuCalllogStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Zcom_Menu_Profiles},
        .selectCB    = zcom_uiProfilesSettingStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Zcom_Menu_Setup},
        .selectCB    = uiMenuSetupStart,
        .selectCBarg = NULL
    },
    {
        .text        = {LANG_Zcom_Menu_Network},
        .selectCB    = uiMenuNetworkStart,
        .selectCBarg = NULL
    },
	{
		.text        = {LANG_Gtalk_Google_Talk},
		.selectCB    = uiMenuGtalkStart,
		.selectCBarg = NULL
	},
//    {
//        .text        = {LANG_Zcom_Menu_Flash_Write},
//        .selectCB    = zcom_uiTestFlashWrite,
//        .selectCBarg = NULL
//    },
//    {
//        .text        = {LANG_Zcom_Menu_Flash_Read},
//        .selectCB    = zcom_uiTestFlashRead,
//        .selectCBarg = NULL
//    },

	{
		.text        = {LANG_Menu_IM_title},
		.selectCB    = zcom_uiIMMainStart,
		.selectCBarg = NULL
	}, //by Mike
};

static const UIMenuListInit MenuMain = {
    /*
     * Main menu title
     */
    .title = { LANG_Zcom_Menu_Main_title },
    /*
     * Number of menu nodes
     */
    .nNodes = sizeof(MenuMainNodes) / sizeof(MenuMainNodes[0]),
    /*
     * Current node.  0 => top
     */
    .currNode = 0,
    /*
     * Display lines.  0 => dynamic
     */
    .nDispLines = 0,
    /*
     * Menu nodes
     */
    .nodes = MenuMainNodes
};

void uiMenuMainStart(void *data)
{
    uiMenuListInit(&MenuMain);
}

/* Standard Soft Keys *********************************************************/
static inline void uiMenuSoftKey(
        UIMenuNode *node,
        LangIdx strIdx
        )
{
    node->text.textIdx   = strIdx;
    node->text.textCB    = NULL;
    node->text.textCBarg = NULL;
    node->selectCB       = NULL;
    node->selectCBarg    = NULL;
}

static inline void uiMenuSoftKeyCB(
        UIMenuNode *node,
        LangIdx strIdx,
        UICBSelect *selectCB,
        void *selectCBarg
        )
{
    node->text.textIdx   = strIdx;
    node->text.textCB    = NULL;
    node->text.textCBarg = NULL;
    node->selectCB       = selectCB;
    node->selectCBarg    = selectCBarg;
}

/* uiMenuSoftKeyPositive -- set soft key labelling for positive action.
 * No callback will be used.
 * A positive action is e.g. "select".
 * May be zero if no label is to be shown.
 * For hardware with 2 soft keys:
 * The labelling will appear on left or right depending upon the hardware
 * configuration.
 * For hardware with 1 soft key: label appears in middle.
 * For hardware with no soft keys: no label appears.
 */
void uiMenuSoftKeyPositive(
        UIMenuNode *nodes,
        LangIdx strIdx
        )
{
    #if KEYPAD_SOFTKEY_NUM >= 1
    #if KEYPAD_POSITIVE_SOFTKEY_POSITION < 0
    #error Bad value for KEYPAD_POSITIVE_SOFTKEY_POSITION
    #endif
    uiMenuSoftKey( nodes+KEYPAD_POSITIVE_SOFTKEY_POSITION, strIdx );
    #endif
}


/* uiMenuSoftKeyNegative -- set soft key labelling for negative action.
 * No callback will be used.
 * A negative action is e.g. "back", "cancel", "clear".
 * May be zero if no label is to be shown.
 * For hardware with 2 soft keys:
 * The labelling will appear on left or right depending upon the hardware
 * configuration.
 * For hardware with 1 or 0 soft key: no label appears (there is
 * no "negative" soft key).
 */
void uiMenuSoftKeyNegative(
        UIMenuNode *nodes,
        LangIdx strIdx
        )
{
    #if KEYPAD_SOFTKEY_NUM >= 2
    #if KEYPAD_NEGATIVE_SOFTKEY_POSITION < 0 || KEYPAD_NEGATIVE_SOFTKEY_POSITION == KEYPAD_POSITIVE_SOFTKEY_POSITION
    #error Bad value for KEYPAD_NEGATIVE_SOFTKEY_POSITION
    #endif
    uiMenuSoftKey( nodes+KEYPAD_NEGATIVE_SOFTKEY_POSITION, strIdx );
    #endif
}


/* uiMenuSoftKeyCBPositive -- set soft key labelling for positive action.
 * No callback will be used.
 * A positive action is e.g. "select".
 * May be zero if no label is to be shown.
 * For hardware with 2 soft keys:
 * The labelling will appear on left or right depending upon the hardware
 * configuration.
 * For hardware with 1 soft key: label appears in middle.
 * For hardware with no soft keys: no label appears.
 */
void uiMenuSoftKeyCBPositive(
        UIMenuNode *nodes,
        LangIdx strIdx,
        UICBSelect *selectCB,
        void *selectCBarg
        )
{
    #if KEYPAD_SOFTKEY_NUM >= 1
    #if KEYPAD_POSITIVE_SOFTKEY_POSITION < 0
    #error Bad value for KEYPAD_POSITIVE_SOFTKEY_POSITION
    #endif
    uiMenuSoftKeyCB(
        nodes+KEYPAD_POSITIVE_SOFTKEY_POSITION, strIdx, selectCB, selectCBarg);
    #endif
}


/* uiMenuSoftKeyCBNegative -- set soft key labelling for negative action.
 * No callback will be used.
 * A negative action is e.g. "back", "cancel", "clear".
 * May be zero if no label is to be shown.
 * For hardware with 2 soft keys:
 * The labelling will appear on left or right depending upon the hardware
 * configuration.
 * For hardware with 1 or 0 soft key: no label appears (there is
 * no "negative" soft key).
 */
void uiMenuSoftKeyCBNegative(
        UIMenuNode *nodes,
        LangIdx strIdx,
        UICBSelect *selectCB,
        void *selectCBarg
        )
{
    #if KEYPAD_SOFTKEY_NUM >= 2
    #if KEYPAD_NEGATIVE_SOFTKEY_POSITION < 0 || KEYPAD_NEGATIVE_SOFTKEY_POSITION == KEYPAD_POSITIVE_SOFTKEY_POSITION
    #error Bad value for KEYPAD_NEGATIVE_SOFTKEY_POSITION
    #endif
    uiMenuSoftKeyCB(
        nodes+KEYPAD_NEGATIVE_SOFTKEY_POSITION, strIdx, selectCB, selectCBarg);
    #endif
}
